
package sample.multithreading;

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import compatible.actionbar.ActionBarFragmentActivity;
import compatible.actionbar.CompatibleActionBar;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

public class DownloaderActivity extends ActionBarFragmentActivity
{
    private static final int NUM_LINES = 100;
    ArrayAdapter<URL> mAdapter;
    String[] mListItems = new String[NUM_LINES];
    ListView mListView;
    Intent mServiceIntent;
    int mTotalLines;
    TextView mActionBarProgressText;
    NetworkDownloadTask mDownloader;
    private static final String PICASA_RSS_URL = "http://picasaweb.google.com/data/feed/base/featured?alt=rss&kind=photo&access=public&slabel=featured&hl=en_US&imgmax=1600";
    
    void setProgressText(int paramInt) {
        if (this.mActionBarProgressText != null)
            this.mActionBarProgressText.setText(paramInt);
    }
    
    public void showProgress(boolean paramBoolean) {
        getCompatibleActionBar().showCustomView(paramBoolean);
    }
    
    public ListView getListView()
    {
        if (this.mListView == null) {
            this.mListView = ((ListView) findViewById(android.R.id.list));
        }
        return this.mListView;
    }

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        CompatibleActionBar localCompatibleActionBar = getCompatibleActionBar();
        
        setContentView(android.R.layout.list_content);
        
        localCompatibleActionBar.setBackgroundDrawable(getResources()
                .getDrawable(R.drawable.actionbarbg));
        localCompatibleActionBar.setDisplayUseLogoEnabled(true);
        localCompatibleActionBar.setLogo(R.drawable.icon);
        View localView = getLayoutInflater().inflate(R.layout.progress, null);
        localCompatibleActionBar.setCustomView(localView);
        this.mActionBarProgressText = ((TextView) localView
                .findViewById(R.id.actionBarProgressText));
        IntentFilter filter = new IntentFilter(NetworkDownloadTask.BROADCAST_ACTION);
        filter.addCategory(Intent.CATEGORY_DEFAULT);        
    }

    @Override
    public void onStart() {
// Step 1: Uncomment this code
//
//        URL localUrl;
//        try {
//            localUrl = new URL(PICASA_RSS_URL);
//            mDownloader = new NetworkDownloadTask(this);
//            mDownloader.execute(localUrl);
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
        super.onStart();
    }

    @Override
    public void onStop() {
// Step 2: Uncomment this code
//        if ( null != mDownloader ) {
//            mDownloader.cancel(true);
//            mDownloader = null;
//        }        
        super.onStop();
    }
    
    @Override
    public void onDestroy()
    {
        this.mListView = null;
        this.mAdapter = null;
        super.onDestroy();
    }
    
    void setResult(Vector<URL[]> result) {
        if ( null != result ) {
            int size = result.size();
            URL[] urlArray = new URL[size];
            for ( int i = 0; i < size; i++ ) {
                urlArray[i] = result.get(i)[0];
            }
            mAdapter = new ArrayAdapter<URL>(this, R.layout.logitem, urlArray);
            getListView().setAdapter(mAdapter);
        }
    }
}
